package com.test.demo.main.activity

import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.graphics.ColorUtils
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.Jzvd
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.google.android.material.appbar.AppBarLayout
import com.gyf.immersionbar.ImmersionBar
import com.test.demo.R
import com.test.demo.base.BaseActivity
import com.test.demo.databinding.ActivityTopicDetailsBinding
import com.test.demo.main.activity.dynamic.DynamicDetailActivity
import com.test.demo.main.adapter.TopicDetailsAdapter
import com.test.demo.main.bean.ItemTopicListBean
import com.test.demo.main.bean.MediumBean
import com.test.demo.main.data.MainViewModel
import com.test.demo.utils.VideoPlayActivity
import com.test.demo.utils.data.ConstUtils
import kotlin.math.abs


/**
 * 话题页面
 */
class TopicDetailsActivity : BaseActivity<MainViewModel, ActivityTopicDetailsBinding>() {

    override fun layoutId(): Int {
        return R.layout.activity_topic_details
    }

    var topicDetailsAdapter: TopicDetailsAdapter? = null
    var linearLayoutManager: LinearLayoutManager? = null
    var video_url =
        "https://asset.test.pandaball.cc/upload/video/b7d013b3408dd98fe788de010938222e.mp4"

    override fun initView(savedInstanceState: Bundle?) {
        initAdapterDataView()
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initListener() {
        super.initListener()

        binding.refreshView.setOnRefreshListener { layout ->
            // 模拟网络请求
            Handler().postDelayed({
                layout.finishRefresh()  // 结束刷新
            }, 2000)
        }

        binding.appbar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
            val totalScrollRange = appBarLayout.totalScrollRange
            if (verticalOffset == 0) {
                // 顶部状态
                binding.centerText.visibility = View.GONE
            } else if (abs(verticalOffset) >= appBarLayout?.totalScrollRange!!) {
                // 完全折叠状态
                binding.centerText.visibility = View.VISIBLE
            } else {
                // 滚动中，显示文本
                binding.centerText.visibility = View.GONE
            }
            // 计算折叠的比例
            val ratio = Math.abs(verticalOffset).toFloat() / totalScrollRange.toFloat()
            // 使用 ColorUtils 来混合背景颜色，从透明到纯白
            val color = ColorUtils.blendARGB(Color.TRANSPARENT, Color.WHITE, ratio)
            // 设置渐变背景
            binding.toolbar.setBackgroundColor(color)
        })

        topicDetailsAdapter?.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                DynamicDetailActivity.getInstance(
                    context = this@TopicDetailsActivity,
                    listData[position].urlList as ArrayList<MediumBean>
                )
            }
        })

        topicDetailsAdapter?.setItem(object : TopicDetailsAdapter.onItemRemotePlay {
            override fun onclick(url: String?) {
                var bundle = Intent()
                bundle.setClass(this@TopicDetailsActivity, VideoPlayActivity::class.java)
                bundle.putExtra(ConstUtils.VIDEO_PLAY_URL, url)
                startActivity(bundle)
            }
        })
    }

    var listData: MutableList<ItemTopicListBean> = arrayListOf()
    private fun initAdapterDataView() {
        topicDetailsAdapter = TopicDetailsAdapter()
        binding.recyclerView.adapter = topicDetailsAdapter
        linearLayoutManager = LinearLayoutManager(this)
        binding.recyclerView.layoutManager = linearLayoutManager

        var mediumListBean: MutableList<MediumBean> = arrayListOf()
        mediumListBean.add(MediumBean(type = 1, resId = R.drawable.topic1))

        var mediumListBean4: MutableList<MediumBean> = arrayListOf()
        mediumListBean4.add(MediumBean(type = 1, resId = R.drawable.two_people))
        mediumListBean4.add(MediumBean(type = 1, resId = R.drawable.topic4))

        var mediumListBean1: MutableList<MediumBean> = arrayListOf()
        mediumListBean1.add(MediumBean(type = 2, url = "$video_url"))

        listData.add(0, ItemTopicListBean(urlList = mediumListBean4))
        listData.add(1, ItemTopicListBean(urlList = mediumListBean))
        listData.add(2, ItemTopicListBean(urlList = mediumListBean1))
        var mediumListBean3: MutableList<MediumBean> = arrayListOf()
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.two_people))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.topic4))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.topic3))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.two_people))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.topic2))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.topic2))
        mediumListBean3.add(MediumBean(type = 1, resId = R.drawable.topic3))
        for (i in 3..10) {
            listData.add(i, ItemTopicListBean(urlList = mediumListBean3))
        }
        topicDetailsAdapter?.setList(listData)
    }

    override fun onResume() {
        super.onResume()
        Handler().postDelayed({
            startVideo()
        }, 500)
    }

    /**
     * 视频开始播放
     * 恢复播放
     */
    private fun startVideo() {
        if (topicDetailsAdapter?.getGSYVideoPlayer() != null) {
            topicDetailsAdapter?.getGSYVideoPlayer()?.startVideo()
            Jzvd.goOnPlayOnResume()
        }
    }

    /**
     * 判断 View 是否在屏幕可见区域内
     */
    private fun viewIsVisible(view: View): Boolean {
        val location = IntArray(2)
        view.getLocationOnScreen(location)

        val screenHeight = Resources.getSystem().displayMetrics.heightPixels  // 屏幕高度
        val viewTop = location[1]  // 视图顶部位置
        val viewBottom = viewTop + view.height  // 视图底部位置
        // 视图的顶部或底部至少有一部分在屏幕内
        return viewBottom > 0 && viewTop < screenHeight
    }


    override fun onPause() {
        super.onPause()
        //播放器销毁
        stopVideoPlay()
    }

    /**
     * 视频停止
     */
    fun stopVideoPlay() {
        Jzvd.goOnPlayOnPause()
    }

    /**
     * 子类单独继承修改状态栏
     */
    override fun initStatusBar() {
        super.initStatusBar()

        ImmersionBar.with(this)
            .transparentStatusBar()
            .transparentNavigationBar()
            .statusBarDarkFont(false).init()
    }


}